<?php

/*
 * Create by Indonesian Brain Solutions
 * E-mail : cs@mesinit.com
 * Programmer : sony@sonywibisono.com
 */
if (!defined('__BACKEND__'))
    die("you cannot directly access this page");

include ("class/AppRoles.php");
global $action,$user_id,$login_status,$is_admin,$mod;
_showMenuHeader();

function add(){
    echo "<h2>Add Record</h2>
        <form action=\"?mod=admin&file=roles\" method=\"post\">    
    <input type=\"hidden\" name=\"action\" value=\"add_process\"/>
<table cellpadding=\"4\" cellspacing=\"0\">
<tr><td class=\"field_name\">Id</td><td class=\"field_object\"># </td></tr>
<tr><td class=\"field_name\">Role Name</td><td class=\"field_object\"><input type=\"text\" name=\"role_name\" class=\"textbox\"/> </td></tr>
<tr><td class=\"field_name\">Role Desc</td><td class=\"field_object\"><input type=\"text\" name=\"role_desc\" class=\"textbox\"/> </td></tr>
<tr><td></td><td><input class=\"button\" type=\"submit\" value=\"Add New Role\"/>
        <input class=\"button\" type=\"button\" name=\"cancel\" 
        value=\"Cancel\" onclick=\"javascript:history.go(-1);\"/></td></tr></table></form>";
}
function edit(){
    global $db,$id;
    if (!empty($id)){
    $sql="select * from app_roles where id=$id";
    $row=$db->fetch_row($sql);
    extract($row);
    echo "<h2>Edit Record</h2>
        <form action=\"?mod=admin&file=roles\" method=\"post\">    
    <input type=\"hidden\" name=\"action\" value=\"edit_process\"/>
<table cellpadding=\"4\" cellspacing=\"0\">
<tr><td class=\"field_name\">Id</td><td class=\"field_object\">
<input type=\"hidden\" name=\"id\" value=\"$id\"/>#$id </td></tr>
<tr><td class=\"field_name\">Role Name</td>
<td class=\"field_object\"><input type=\"text\" name=\"role_name\" class=\"textbox\" value=\"$role_name\"/> </td></tr>
<tr><td class=\"field_name\">Role Desc</td><td class=\"field_object\">
<textarea rows=\"5\" cols=\"50\" name=\"role_desc\">$role_desc</textarea> </td></tr>
<tr><td></td><td><input class=\"button\" type=\"submit\" value=\"Update\"/>
        <input class=\"button\" type=\"button\" name=\"cancel\" 
        value=\"Cancel\" onclick=\"javascript:history.go(-1);\"/></td></tr></table></form>";
    }
}
function add_process(){
    global $db,$id,$role_name,$role_desc,$mod,$file;
    echo "Add Process Record";
    if (!empty($role_name)){
        $roles=new AppRoles($id,$role_name, $role_desc);
        $roles->insert();
        $url="?mod=$mod&file=$file";
        $msg="Insert Record Success";
    
        refresh_to($url, $msg, 2);
    }
}
function edit_process(){
    global $db,$id,$role_name,$role_desc,$mod,$file;
    echo "Edit Process Record";
    if(!empty($role_name)){
        $roles=new AppRoles($id,$role_name, $role_desc);
        $roles->update();
        $url="?mod=$mod&file=$file";
        $msg="Update Record Success";
    
        refresh_to($url, $msg, 2);
    }
}
function delete_process(){
    global $db,$id,$mod,$file;
    if (!empty($id)){
    echo "Delete Process Record";
    $sql="delete from app_roles where id=$id";
    $db->query($sql);
    $url="?mod=$mod&file=$file";
    $msg="Delete Record Success";
    
    refresh_to($url, $msg, 2);
    }
}
function __default(){
    global $db,$page,$sortby,$sortorder,$mod,$pagesize,$file;
    if (empty($page)){
        $start=0;
        $sql="select *  from app_roles
            order by role_name";
    } else {
        $start=$page*$pagesize;
        $sql="select *  from app_modules
            order by role_name limit $start,$pagesize";
    }
   
    
    $query=$db->query($sql);
    echo "<h2>Role List</h2>
        <div id=\"caption_table\"><a class='action_link' href=\"?mod=$mod&file=menus&action=add\">Add New</a></div>";
    echo "<div id=\"table_data\">
        <table width=\"100%\" cellpadding=0 cellspacing=0>
        <tr><th class='table_left_top'>Id</th><th>Role Name</th> 
        <th>Description</th><th class='table_right_top'>Action</th></tr>";
    $i=0;
    while ($row=$db->fetch_array($query)){
        extract($row);
        if ($i%2==0){
            $class="row_even";
        } else {
            $class="row_odd";
        }
        echo "<tr><tr class=\"$class\"><td align='right'>$id</td><td>$role_name</td><td>$role_desc</td>
             
                <td align=center><a href=\"?mod=$mod&file=$file&action=edit&id=$id\" class=\"icon_link\">
                    <img class='icon_small' src='images/edit.png' title='Edit'/></a>
        <a href=\"?mod=$mod&file=$file&action=delete&id=$id\" class=\"icon_link\">
            <img class='icon_small' src='images/delete.png' title='Delete'/></a></td></tr>";
    
        $i++;
    }
    echo "<tr class='row_odd'><td class=\"table_left_bottom\"></td><td colspan='2'>&nbsp;</td><td class=\"table_right_bottom\"></td></tr>";
    echo "</table></div>";
}
switch($action){
    case 'add':
        add();
        break;
    case 'edit':
        edit();
        break;
    case 'add_process':
        add_process();
        break;
    case 'edit_process':
        edit_process();
        break;
    case 'delete':
        delete_process();
        break;
    default:
        __default();
}
?>